﻿@model HWmanagementClient.Models.TeacherHomeworks
@{
    ViewBag.Title = "Eredmény írása";
}
<h2>
    Eredmény írása</h2>
<script type="text/javascript">
    $(function () {
        $("#studentcourses").accordion();
    });

    var markup = '<tr id="trashable">' +
                        '<td><strong>${title}</strong> (ID: ${id}, C: ${courseid}, U: ${userid})' +
                        '<br>' +
                        '<em>Beadási határidő: <strong>${deadline2}</strong></em></td>' +
                        '<td>' +
                        '<a href="#" class="buttonclass" style="color: white;" onclick="getusers(${id});"> >>> </a>' +
                        '</td>' +
                    '</tr>';

    var markup2 = '<a id="trashable" href="#" class="buttonclass" style="color: white;" onclick="writeresult(\'${Neptun}\');">${Name}</a>';
    var markup3 = '<a id="trashable2" target="_blank" href="${fileurl}" >${filename}</a>' +
    '{{if last == 1}}' +
    '(utolsó verzió)<br>' +
    '{{else}}' +
    '<br>' +
    '{{/if}}';


    function getHomework(id) {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/Teacher/GetGrade/" + id,
            dataType: "json",
            async: false
        })
			.done(function (msg) {
			    var hw = msg.Homeworks;
			    //alert(hw);
			    $("tr#trashable").remove();
			    $.template("hw_templ", markup);
			    $("#hwcontainer").before($.tmpl("hw_templ", hw));
			    //$.tmpl("hw_templ", hw).appendTo("#hwcontainer");
			    $("a.buttonclass").button();
			});
    }

    function getusers(hwid) {

        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/Teacher/GetGroupUsers/" + hwid,
            dataType: "json",
            async: false
        })
			.done(function (msg) {
			    var hw = msg.users;
			    //alert(hw);
			    $("a#trashable").remove();
			    $.template("u_templ", markup2);
			    $("#ucontainer").before($.tmpl("u_templ", hw));
			    //$.tmpl("hw_templ", hw).appendTo("#hwcontainer");
			    $("a.buttonclass").button();
			});

        $("#ihwid").val(hwid);
        $("#selecthw").hide("slow");
        $("#selectuser").show("slow");
    }

    function ungetusers() {
        $("#ihwid").val(0);
        $("#iuserid").val(0);
        $("#selecthw").show("slow");
        $("#selectuser").hide("slow");
    }

    function writeresult(userid) {
        $("#iuserid").val(userid);
        $("input[type=submit]").button();

        $("#dialog-form").dialog({
            modal: true,
            height: 600,
            width: 800,
            buttons: {
                Mégse: function () {
                    $(this).dialog("close");
                }
            }
        });

        $.ajax({
            type: "POST",
            url: "/Teacher/GetUFiles/",
            data: { userid: $("#iuserid").val(), hwid: $("#ihwid").val() },
            async: false
        })
			.done(function (msg) {
			    var hw = msg.files;
			    //alert(hw);
			    $("a#trashable2").remove();
			    $.template("f_templ", markup3);
			    $("#filecontainer").before($.tmpl("f_templ", hw));
			    //$.tmpl("hw_templ", hw).appendTo("#hwcontainer");
			});

			$.ajax({
			    type: "POST",
			    url: "/Teacher/GetGGrade/",
			    data: { userid: $("#iuserid").val(), hwid: $("#ihwid").val() },
			    async: false
			})
			.done(function (msg) {
			    if (msg.grade != 0) {
			        $("#newresult_grade").val(msg.grade);
			    }
			    $("#newresult_comment").val(msg.comment);
			});
    }

</script>
@if (ViewBag.Succeeded == true)
{
    <div class="ui-widget">
        <div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;">
            <p>
                <span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
                <strong>OK!</strong> Az értékelés sikeres volt.</p>
        </div>
    </div>
    <br />
    <br />
}
else if (ViewBag.Succeeded == false)
{
    <div class="ui-widget">
        <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
            <p>
                <span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
                <strong>HIBA!</strong> Az értékelés nem sikerült.</p>
        </div>
    </div>
    <br />
    <br />
}
<div id="selecthw">
    <table>
        <tr>
            <td valign="top" style="width: 300px;">
                <div id="studentcourses">
                    @foreach (var item in Model.Subjects)
                    {
                        <h3>
                            @item.name (@item.neptun.ToUpper())</h3>
                        <div>
                            @foreach (var course in item.courses)
                            {
                                <a href="#" onclick="getHomework(@course.id);">[ @course.semester ]</a> 
                            }
                        </div>
                    }
                </div>
            </td>
            <td valign="top">
                <table>
                    <tbody>
                        <tr id="hwcontainer">
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </table>
</div>
<div id="selectuser" style="display: none;">
    <h2>
        Válasszon hallgatót <a href="#" class="buttonclass" style="color: white;" onclick="ungetusers();">
            Befejezés </a>
    </h2>
    <div id="ucontainer">
    </div>
</div>
<div id="dialog-form" title="Értékelés írása" style="display: none;">
    @using (Html.BeginForm("Grades", "Teacher", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div>
            <fieldset>
                <legend>Új értékelés</legend>
                <input type="hidden" name="hwid" value="none" id="ihwid" />
                <input type="hidden" name="userid" value="none" id="iuserid" />
                <div class="editor-label">
                    Érdemjegy*:
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.newresult.grade)
                </div>
                <div class="editor-label">
                    Megjegyzés:
                </div>
                <div class="editor-field">
                    @Html.TextAreaFor(m => m.newresult.comment)
                </div>
                <br />
                <div id="filecontainer">
                </div>
                <br />
                <input type="submit" value="OK" />
            </fieldset>
        </div>
        
    }
</div>
